#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <string.h>

int main(){

printf("Debut du program principal\n");

int x=getpid();
printf("My process id is: %d \n\n", x);

char c[20];
sprintf(c,"pstree -phn %d ", x);
int newid;
int i;
    for (i=0; i<3; i++){
	newid = fork();
	switch(newid)
	{
		case 0:
			//partie executee en cas de success du fork
			fprintf(stdout,"==>CHILD PID=%d\n\n",getpid());
			if (execl("/usr/bin/whereis","whereis","cat", NULL)==-1 )
			{
				 perror("execl error");
			}
			break;
		case -1:
			//partie executee en cas d eche du fork
			fprintf(stdout,"==>FAILED to create a child process \n\n");
		        perror("fork error");
			exit(1);
			break;
		default:
			//le programm principal
			printf("Fin du program principal\n\n");
			break;
	}//switch
        printf("wykonujemy: %s \n", c);
        system(c);
 }//for

    for(i=0; i<3; i++){
	//le process parent doit attendre WAIT les processus enfants 
        if (newid){
            int w;
            pid_t pid_pot;
            pid_pot= wait(&w);
            printf("Proces potomny PID =%d zakonczyl prace \n",pid_pot);
            printf("Kod powrotu procesu potomnego: %d\n", w);
        }
     }//for
    
return 0;
}

